package tcpdump.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import tcpdump.demo.entity.Result;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@RestController
@RequestMapping("/tcp")
public class TcpDumpController {
  private static final Logger log = LoggerFactory.getLogger(TcpDumpController.class);
  
  @GetMapping("/get/{msg}")
  public Result get(@PathVariable("msg") String msg) {
    HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    log.info("~~ headers start print");
    
    Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
    for (; headerNames.hasMoreElements(); ) {
      String headerName = headerNames.nextElement(), headerValue = httpServletRequest.getHeader(headerName);
      log.info("~~ {} : {}", headerName, headerValue);
    }
    
    log.info("~~ headers end print");
    
    log.info("request info /get/{}", msg);
    return Result.ok(msg);
  }
}
